package cn.ydt.ydt.resource.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
    //@Value可以将配置文件的内容自动注入到属性内
    @Value("${SavePath.uploadPath}")
    private String uploadPath;   //图片物理存储路径
    @Value("${SavePath.getPath}")
    private String getPath;   //图片映射路径

//    获取文件配置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
//        所有getPath开头的请求都会去后面配置的路径下查找资源
        registry.addResourceHandler(getPath+"**").addResourceLocations("file:"+uploadPath);
    }



    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }



}
